;
(function (window, angular) {
    'use strict';
    angular.module("Controller.BasicData.PrintTemplate", [])
        .controller("PrintTemplateCtrl", ["$scope", "$state", "ybHttp", "ybWidget", "MESG_TYPE", "UserContextService", "TEMPLATE_CODE", function ($scope, $state, ybHttp, ybWidget, MESG_TYPE, UserContextService, TEMPLATE_CODE) {
            $scope.showChildView = false;
            $scope.activeFlag = [];
            var currentTemplateType = "";

            // 入库单模板
            $scope.inTempList = [
                {
                    tempTitle: '采购入库单',
                    tempType: 'PurchaseIn',
                },
                {
                    tempTitle: '其它入库单',
                    tempType: 'UnusualIn',
                },
                {
                    tempTitle: '期初入库单',
                    tempType: 'BeginningIn',
                },
                {
                    tempTitle: '退货入库单',
                    tempType: 'ReturnIn',
                },
                {
                    tempTitle: '领料入库单',
                    tempType: 'PickingIn',
                },
                {
                    tempTitle: '配送指令入库单',
                    tempType: 'DeliveryOrderIn',
                },
                {
                    tempTitle: '回购入库单',
                    tempType: 'BackIn',
                },
                {
                    tempTitle: '运单入库单',
                    tempType: 'ReceiptIn',
                }
            ];

            // 出库单模板
            $scope.outTempList = [
                {
                    tempTitle: '发货通知单',
                    tempType: 'SaleOrder',
                },
                {
                    tempTitle: '其它出库单',
                    tempType: 'UnusualOut',
                },
                {
                    tempTitle: '三包出库单',
                    tempType: 'GuaranteesOut',
                },
                {
                    tempTitle: '赠送出库单',
                    tempType: 'ComplimentaryOut',
                },
                {
                    tempTitle: '领料出库单',
                    tempType: 'PickingOut',
                },
                {
                    tempTitle: '发运交接单',
                    tempType: 'Handover',
                },
                {
                    tempTitle: '发运交接单',
                    tempSubTitle: '装箱清单',
                    tempType: 'Encasement',
                },
                {
                    tempTitle: '发运交接单',
                    tempSubTitle: '装箱清单明细',
                    tempType: 'EncasementD',
                },
                {
                    tempTitle: '发运交接单',
                    tempSubTitle: '装箱清单-进出口专用',
                    tempType: 'Encasement4JCK',
                },
                {
                    tempTitle: '成品发运单',
                    tempType: 'BoxupNoPack',
                },
                {
                    tempTitle: '回购出库单',
                    tempType: 'BackOut',
                },
                {
                    tempTitle: '销售出库单',
                    tempType: 'SalesOrderOut',
                },
                {
                    tempTitle: '销售出库单',
                    tempSubTitle: '海外模板',
                    tempType: 'SalesOrderOutJck',
                },
                {
                    tempTitle: '运单维护',
                    tempType: 'WaybillMaintenance',
                },
                {
                    tempTitle: '运费对账',
                    tempType: 'WaybillReconciliation',
                },
            ];

            // 其他模板
            $scope.otherTempList = [
                {
                    tempTitle: '退货申请单',
                    tempType: 'ReturnApply',
                },
                {
                    tempTitle: '客户间调拨单',
                    tempType: 'Allocation',
                },
                {
                    tempTitle: '盘点单',
                    tempType: 'Inventory',
                },
                {
                    tempTitle: '区域调拨单',
                    tempType: 'DepotAllocation',
                },
                {
                    tempTitle: '销售冲销单',
                    tempType: 'SaleOffset',
                },
                {
                    tempTitle: '拆件单',
                    tempType: 'SplitPrint',
                },
                {
                    tempTitle: '拆件恢复单',
                    tempType: 'RestorePrint',
                },
                {
                    tempTitle: '送货单',
                    tempType: 'SendPrint',
                },
                {
                    tempTitle: '三包冲销单',
                    tempType: 'ThreeOffset',
                },
                {
                    tempTitle: '服务借用单',
                    tempType: 'ServiceBorrowOut',
                },
                {
                    tempTitle: '借用退回单',
                    tempType: 'ServiceBorrowIn',
                },
                {
                    tempTitle: '退货单',
                    tempType: 'AgentReturnOrder',
                },
                {
                    tempTitle: '收款单',
                    tempType: 'CheckReceive',
                },
                {
                    tempTitle: '结算单',
                    tempType: 'PrePay',
                },
                {
                    tempTitle: '销售开票审批单',
                    tempType: 'MakeOutInvoice',
                },
                {
                    tempTitle: '借用单',
                    tempType: 'BorrowIn',
                },
                {
                    tempTitle: '归还单',
                    tempType: 'BorrowBack',
                },
                {
                    tempTitle: '兑换出库单',
                    tempType: 'GiftOut',
                },
                {
                    tempTitle: '社会渠道申请单',
                    tempType: 'SocialApply',
                },
                {
                    tempTitle: '销售订单',
                    tempType: 'SellIntention',
                },
                {
                    tempTitle: '合同协议单',
                    tempType: 'ContractAgreement',
                },
                {
                    tempTitle: '经销商差价核算',
                    tempType: 'Distributormargin',
                },
                {
                    tempTitle: '劳务费用结算',
                    tempType: 'SettlementOfServiceExpenses',
                },
            ];

            var btnPermissionCode = {
                printTplAdd: window.ActionCode.printTplAdd,
                printTplEdit: window.ActionCode.printTplEdit,
                printTplDel: window.ActionCode.printTplDel,
                printTplDefault: window.ActionCode.printTplDefault
            };

            $scope.btnPermission = {
                printTplAdd: true,
                printTplEdit: true,
                printTplDel: true,
                printTplDefault: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.changeTab = function (index) {
                if (index == 0) {
                    $scope.tempTpls = [];
                }
                for (var i = 0, len = $scope.activeFlag.length; i < len; i++) {
                    $scope.activeFlag[i] = false;
                }
                $scope.activeFlag[index] = true;
            };


            $scope.tempTpls = [];

            //获取模板列表
            function getTplList() {
                $scope.tempTpls = [];

                var param = {
                    CurPage: 1,
                    PageSize: 0,
                    TemplateType: TEMPLATE_CODE[currentTemplateType]
                };
                return ybHttp.Get("device", "/printTemplate/list", param).then(function (data) {
                    if (data.code != 0) {
                        ybWidget.ShowMessage(data);
                        return;
                    }
                    Array.prototype.push.apply($scope.tempTpls, data.value);
                    return data.value;
                })
            }

            $scope.getTemplatesByType = function (index, type) {
                $scope.changeTab(index);
                currentTemplateType = type;
                getTplList();
            };

            //操作-设为默认
            $scope.selectDefault = function (item) {
                if (item.isDefault) {
                    return;
                }
                $scope.tempTpls.forEach(tItem => { tItem.isDefault = false; });
                item.isDefault = true;

                ybHttp.Post("device", "/printTemplate/setDefault", { id: item.id }).then(function (data) {
                    if (data.code != 0) {
                        ybWidget.ShowMessage(data);
                        return;
                    }
                    ybWidget.ShowToaster("success", "操作提示", "操作成功");
                });
            };

            //操作-新增
            $scope.addTemplate = function (type) {
                var param = {
                    printType: type,
                    templateTypeCode: TEMPLATE_CODE[currentTemplateType]
                };
                $state.go('app.basicData.printTemplate.addTemplate', param);
            };

            //操作-编辑
            $scope.update = function (type, id) {
                var param = {
                    id: id,
                    printType: type,
                    templateTypeCode: TEMPLATE_CODE[currentTemplateType]
                };
                $state.go('app.basicData.printTemplate.editTemplate', param);
            };

            //操作-删除
            $scope.deleteTemp = function (id, idx) {
                ybWidget.ShowMessage("确定要删除模板吗？", MESG_TYPE.Ask).then(function (data) {
                    ybHttp.Post("device", "/printTemplate/remove", { id: id }).then(function (data) {
                        if (data.code != 0) {
                            ybWidget.ShowMessage(data);
                            return;
                        }
                        $scope.tempTpls.splice(idx, 1);
                        ybWidget.ShowToaster("success", "操作提示", "操作成功");
                    });
                });
            };

            $scope.$on("showListContent", function (event, showList) {
                $scope.showChildView = !showList;
            });

            $scope.$on('updateTemplateList', function (event) {
                getTplList();
            });

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])

})(window, window.angular);